package com.java.al.swing.filechooser;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class SaveFileChooser extends JFrame implements ActionListener {
	public SaveFileChooser() {
		setTitle("保存");
		setSize(300, 400);
		JMenuBar mbar = new JMenuBar();
		JMenu m = new JMenu("File");
		openItem = new JMenuItem("save");
		openItem.addActionListener(this);
		m.add(openItem);
		exitItem = new JMenuItem("Exit");
		exitItem.addActionListener(this);
		m.add(exitItem);
		mbar.add(m);
		Container contentPane = getContentPane();
		contentPane.add(mbar, "North");
	}

	public void actionPerformed(ActionEvent evt) {
		Object source = evt.getSource();
		if (source == openItem) {
			JFileChooser chooser = new JFileChooser();
			int r = chooser.showSaveDialog(this);
			File f = chooser.getSelectedFile();
			String fname = chooser.getName(f);
			if (r == JFileChooser.APPROVE_OPTION) {
				if (f.isFile())
					fname = f.getName();
				f = chooser.getCurrentDirectory();
				f = new File(f.getPath().concat(File.separator).concat(fname));
				if (f.exists()) {
					int i = javax.swing.JOptionPane.showConfirmDialog(chooser,
							"该文件已经存在，确定要覆盖吗？");
					if (i == javax.swing.JOptionPane.YES_OPTION)
						;
					else
						return;
				}
			}
		} else if (source == exitItem)
			System.exit(0);
	}

	public static void main(String[] args) {
		Frame f = new SaveFileChooser();
		f.setVisible(true);
	}

	private JMenuItem openItem;
	private JMenuItem exitItem;
}